package control;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;

import factory.ComponentInterface;

/**
 * 
 * @author albertcsani & ddenesdemeter
 * 
 */
public class ImageCreator {
	private Point cont = new Point(12, 3);
	private Point truck = new Point(15, 3);
	private Point poz;
	private Point size;
	private Point rail;

	/**
	 * alapertelmezett konstruktor
	 */

	public ImageCreator() {
	}

	/**
	 * 
	 * @param gc
	 *            GC amire rajzolni kell
	 * @param parent
	 *            a Composite amire rajzolni kell
	 * @param comp
	 *            A kirajzolando stack
	 * @param zoom
	 *            a jelenlegei zoomFactor
	 */
	public void CreateStack(GC gc, Composite parent, ComponentInterface comp, int zoom) {
		poz = new Point(comp.getPosition().getX(), comp.getPosition().getY());
		size = new Point(comp.getSize().getX(), comp.getSize().getY());
		rail = new Point(size.x * zoom, (int) zoom / 2);
		if (size.x < size.y) { // fuggoleges stack
			rail = new Point((int) zoom / 2, size.y * zoom);
			// stack alap
			gc.setBackground(new Color(parent.getDisplay(), 150, 150, 150));
			gc.fillRectangle(poz.x * zoom, poz.y * zoom, comp.getSize().getX() * zoom, comp.getSize().getY() * zoom);

			gc.setForeground(new Color(parent.getDisplay(), 0, 0, 0));
			int rows = (size.y - (truck.x) * 2) / cont.x * 2;
			for (int i = 0; i <= rows; i++) {
				gc.drawLine(poz.x * zoom, (poz.y + truck.x + i * cont.x / 2) * zoom, (poz.x + size.x) * zoom - 1, (poz.y + truck.x + i * cont.x / 2) * zoom);
			}

			int cols = (size.x - (truck.y + 1) * 2) / cont.y;
			for (int i = 0; i <= cols; i++) {
				gc.drawLine(((poz.x + truck.y + 1) + i * cont.y) * zoom, poz.y * zoom, ((poz.x + truck.y + 1) + i * cont.y) * zoom, (poz.y + size.y) * zoom - 1);
			}
			// ut + parkolo
			gc.setBackground(new Color(parent.getDisplay(), 200, 200, 200));

			gc.fillRectangle(poz.x * zoom, poz.y * zoom, size.x * zoom, truck.x * zoom);
			gc.fillRectangle((poz.x + size.x) * zoom, (poz.y + size.y) * zoom, -size.x * zoom, -truck.x * zoom);

			gc.fillRectangle(poz.x * zoom, poz.y * zoom, (truck.y + 1) * zoom, rail.y);
			gc.fillRectangle((poz.x + size.x) * zoom, (poz.y) * zoom, -(truck.y + 1) * zoom, rail.y);
			// fekete sin
			gc.setBackground(new Color(parent.getDisplay(), 0, 0, 0));
			gc.fillRectangle(poz.x * zoom, poz.y * zoom, zoom, rail.y);
			gc.fillRectangle((poz.x + size.x) * zoom, (poz.y) * zoom, -zoom, rail.y);
			// feher sin
			gc.setBackground(new Color(parent.getDisplay(), 255, 255, 255));
			gc.fillRectangle(poz.x * zoom, poz.y * zoom, rail.x, rail.y);
			gc.fillRectangle((poz.x + size.x) * zoom, (poz.y) * zoom, -rail.x, rail.y);

			// //////////////////////////

			gc.fillRectangle((poz.x + 4) * zoom, poz.y * zoom, zoom / 2, 15 * zoom);
			gc.fillRectangle((poz.x + size.x - 4) * zoom - zoom / 2, poz.y * zoom, zoom / 2, 15 * zoom);
			gc.fillRectangle((poz.x + 4) * zoom, (poz.y + size.y - 15) * zoom, zoom / 2, 15 * zoom);
			gc.fillRectangle((poz.x + size.x - 4) * zoom - zoom / 2, (poz.y + size.y - 15) * zoom, zoom / 2, 15 * zoom);
			gc.setForeground(new Color(parent.getDisplay(), 255, 255, 255));
			for (int i = 1; i <= cols - 1; i++) {
				gc.drawLine(((poz.x + truck.y + 1) + i * cont.y) * zoom, poz.y * zoom, ((poz.x + truck.y + 1) + i * cont.y) * zoom, (poz.y + truck.x) * zoom - 1);
				gc.drawLine(((poz.x + truck.y + 1) + i * cont.y) * zoom, (poz.y + size.y - truck.x) * zoom, ((poz.x + truck.y + 1) + i * cont.y) * zoom, (poz.y + size.y) * zoom - 1);
			}

		} else { // vizszintes stack

			gc.setBackground(new Color(parent.getDisplay(), 150, 150, 150));
			gc.fillRectangle(poz.x * zoom, poz.y * zoom, comp.getSize().getX() * zoom, comp.getSize().getY() * zoom);

			gc.setBackground(new Color(parent.getDisplay(), 200, 200, 200));

			gc.setForeground(new Color(parent.getDisplay(), 0, 0, 0));
			int rows = (size.y - (truck.y + 1) * 2) / cont.y;
			for (int i = 0; i <= rows; i++) {
				gc.drawLine(poz.x * zoom, (poz.y + truck.y + 1 + i * cont.y) * zoom, (poz.x + size.x) * zoom - 1, (poz.y + truck.y + 1 + i * cont.y) * zoom);
			}

			int cols = (size.x - truck.x * 2) / cont.x * 2;
			for (int i = 0; i <= cols; i++) {
				gc.drawLine((poz.x + truck.x + cont.x / 2 * i) * zoom, poz.y * zoom, (poz.x + truck.x + cont.x / 2 * i) * zoom, (poz.y + size.y) * zoom - 1);
			}

			gc.setBackground(new Color(parent.getDisplay(), 200, 200, 200));

			gc.fillRectangle(poz.x * zoom, poz.y * zoom, truck.x * zoom, size.y * zoom);
			gc.fillRectangle((poz.x + size.x) * zoom, (poz.y + size.y) * zoom, -truck.x * zoom, -size.y * zoom);

			gc.fillRectangle(poz.x * zoom, poz.y * zoom, rail.x, (truck.y + 1) * zoom);
			gc.fillRectangle(poz.x * zoom, (poz.y + size.y) * zoom, rail.x, -(truck.y + 1) * zoom);

			gc.setBackground(new Color(parent.getDisplay(), 0, 0, 0));
			gc.fillRectangle(poz.x * zoom, poz.y * zoom, rail.x, zoom);
			gc.fillRectangle(poz.x * zoom, (poz.y + size.y) * zoom, rail.x, -zoom);

			gc.setBackground(new Color(parent.getDisplay(), 255, 255, 255));
			gc.fillRectangle(poz.x * zoom, poz.y * zoom, rail.x, rail.y);
			gc.fillRectangle(poz.x * zoom, (poz.y + size.y) * zoom, rail.x, -rail.y);

			gc.fillRectangle(poz.x * zoom, (poz.y + 4) * zoom, zoom * 15, zoom / 2);
			gc.fillRectangle((poz.x) * zoom, (poz.y + size.y - 4) * zoom - zoom / 2, zoom * 15, zoom / 2);
			gc.fillRectangle((poz.x + size.x - 15) * zoom, (poz.y + 4) * zoom, zoom * 15, zoom / 2);
			gc.fillRectangle((poz.x + size.x - 15) * zoom, (poz.y + size.y - 4) * zoom - zoom / 2, zoom * 15, zoom / 2);
			gc.setForeground(new Color(parent.getDisplay(), 255, 255, 255));
			for (int i = 1; i <= rows - 1; i++) {
				gc.drawLine(poz.x * zoom, (poz.y + truck.y + 1 + i * cont.y) * zoom, (poz.x + 15) * zoom - 1, (poz.y + truck.y + 1 + i * cont.y) * zoom);
				gc.drawLine((poz.x + size.x - truck.x) * zoom, (poz.y + truck.y + 1 + i * cont.y) * zoom, (poz.x + size.x) * zoom - 1, (poz.y + truck.y + 1 + i * cont.y) * zoom);

			}
		}

	}

	/**
	 * 
	 * @param gc
	 *            GC amire rajzolni kell
	 * @param parent
	 *            a Composite amire rajzolni kell
	 * @param comp
	 *            A kirajzolando stack
	 * @param zoom
	 *            a jelenlegei zoomFactor
	 */
	public void CreateRubberTiredGantry(GC gc, Composite parent, ComponentInterface comp, int zoom) {
		int posX = comp.getPosition().getX();
		int posY = comp.getPosition().getY();
		int sizeX = comp.getSize().getX();
		int sizeY = comp.getSize().getY();
		if (sizeX > sizeY) {
			gc.setBackground(new Color(parent.getDisplay(), 255, 186, 0));
			gc.fillRectangle(posX * zoom, posY * zoom, sizeX * zoom, 3 * zoom / 2);
			gc.fillRectangle(posX * zoom, posY * zoom, 3 * zoom / 2, sizeY * zoom);
			gc.fillRectangle(posX * zoom, (posY + sizeY) * zoom - 3 * zoom / 2, sizeX * zoom, 3 * zoom / 2);
			gc.fillRectangle((posX + sizeX) * zoom - 3 * zoom / 2, posY * zoom, 3 * zoom / 2, sizeY * zoom);
			gc.setBackground(new Color(parent.getDisplay(), 0, 0, 0));
			gc.fillRectangle(posX * zoom + 3 * zoom / 2, (posY) * zoom + zoom / 2, (sizeX - 3) * zoom, zoom / 2);
			gc.fillRectangle(posX * zoom + 3 * zoom / 2, (posY + sizeY - 1) * zoom, (sizeX - 3) * zoom, zoom / 2);
			gc.setBackground(new Color(parent.getDisplay(), 0, 134, 0));
			gc.fillRectangle((posX + (sizeX / 2)) * zoom - 3 * zoom / 2, posY * zoom, 3 * zoom, sizeY * zoom);

		} else {

			gc.setBackground(new Color(parent.getDisplay(), 255, 186, 0));
			gc.fillRectangle(posX * zoom, posY * zoom, sizeX * zoom, 3 * zoom / 2);
			gc.fillRectangle(posX * zoom, posY * zoom, 3 * zoom / 2, sizeY * zoom);
			gc.fillRectangle(posX * zoom, (posY + sizeY) * zoom - 3 * zoom / 2, sizeX * zoom, 3 * zoom / 2);
			gc.fillRectangle((posX + sizeX) * zoom - 3 * zoom / 2, posY * zoom, 3 * zoom / 2, sizeY * zoom);
			gc.setBackground(new Color(parent.getDisplay(), 0, 0, 0));
			gc.fillRectangle((posX) * zoom + zoom / 2, (posY) * zoom + 3 * zoom / 2, zoom / 2, (sizeY - 3) * zoom);
			gc.fillRectangle((posX + sizeX - 1) * zoom, (posY) * zoom + 3 * zoom / 2, zoom / 2, (sizeY - 3) * zoom);
			gc.setBackground(new Color(parent.getDisplay(), 0, 134, 0));
			gc.fillRectangle(posX * zoom, (posY + (sizeY / 2)) * zoom - 3 * zoom / 2, sizeX * zoom, 3 * zoom);

		}

	}

	/**
	 * 
	 * @param gc
	 *            GC amire rajzolni kell
	 * @param parent
	 *            a Composite amire rajzolni kell
	 * @param comp
	 *            A kirajzolando stack
	 * @param zoom
	 *            a jelenlegei zoomFactor
	 */
	public void CreateRailMountedGantry(GC gc, Composite parent, ComponentInterface comp, int zoom) {
		int posX = comp.getPosition().getX();
		int posY = comp.getPosition().getY();
		int sizeX = comp.getSize().getX();
		int sizeY = comp.getSize().getY();
		if (sizeX > sizeY) {
			gc.setBackground(new Color(parent.getDisplay(), 200, 0, 0));
			gc.fillRectangle(posX * zoom, posY * zoom, sizeX * zoom, 2 * zoom);
			gc.fillRectangle(posX * zoom, posY * zoom, 2 * zoom, sizeY * zoom);
			gc.fillRectangle(posX * zoom, (posY + sizeY - 2) * zoom, sizeX * zoom, 2 * zoom);
			gc.fillRectangle((posX + sizeX - 2) * zoom, posY * zoom, 2 * zoom, sizeY * zoom);

			gc.setBackground(new Color(parent.getDisplay(), 0, 0, 0));
			gc.fillRectangle((posX + 2) * zoom, (posY + 1) * zoom, (sizeX - 4) * zoom, zoom / 2);
			gc.fillRectangle((posX + 2) * zoom, (posY + sizeY - 1) * zoom - zoom / 2, (sizeX - 4) * zoom, zoom / 2);
			gc.setBackground(new Color(parent.getDisplay(), 0, 134, 0));
			gc.fillRectangle((posX + (sizeX / 2 - 1)) * zoom, posY * zoom, 2 * zoom, sizeY * zoom);
			gc.setBackground(new Color(parent.getDisplay(), 0, 84, 0));
			gc.fillRectangle((posX + (sizeX / 2 - 1)) * zoom - zoom / 2, (posY + (sizeY / 2 - 2)) * zoom - zoom / 2, 3 * zoom, 6 * zoom);

		} else {

			gc.setBackground(new Color(parent.getDisplay(), 200, 0, 0));
			gc.fillRectangle(posX * zoom, posY * zoom, sizeX * zoom, 2 * zoom);
			gc.fillRectangle(posX * zoom, posY * zoom, 2 * zoom, sizeY * zoom);
			gc.fillRectangle(posX * zoom, (posY + sizeY - 2) * zoom, sizeX * zoom, 2 * zoom);
			gc.fillRectangle((posX + sizeX - 2) * zoom, posY * zoom, 2 * zoom, sizeY * zoom);
			gc.setBackground(new Color(parent.getDisplay(), 0, 0, 0));
			gc.fillRectangle((posX + 1) * zoom, (posY + 2) * zoom, zoom / 2, (sizeY - 4) * zoom);
			gc.fillRectangle((posX + sizeX - 1) * zoom - zoom / 2, (posY + 2) * zoom, zoom / 2, (sizeY - 4) * zoom);
			gc.setBackground(new Color(parent.getDisplay(), 0, 134, 0));
			gc.fillRectangle(posX * zoom, (posY + (sizeY / 2 - 1)) * zoom, sizeX * zoom, 2 * zoom);
			gc.setBackground(new Color(parent.getDisplay(), 0, 84, 0));
			gc.fillRectangle((posX + (sizeX / 2 - 2)) * zoom - zoom / 2, (posY + (sizeY / 2 - 1)) * zoom - zoom / 2, 6 * zoom, 3 * zoom);

		}

	}
}
